from fastapi import APIRouter, Request
from fastapi import Depends
from config.get_db import get_db
from config.get_scheduler import MyCronTrigger, SchedulerUtil
from core.dao.cms_terminal_point_dao import CmsTerminalPointDao
from core.service.car_track_service import *
from core.service.gaode.gaode_fence_terminal_service import GaodeFenceTerminalService
from module_task.alert_message_service import AlertMessageService
from utils.response_util import *
from utils.log_util import *

monitorController = APIRouter(prefix='/monitor')


@monitorController.post("/car_track/query", response_model=CarTrackModel)
async def get_monitor_server_info(request: Request, car_id: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        car_track_list = CarTrackService.get_car_track_by_car_id(query_db, car_id)
        logger.info('获取成功')
        return response_200(data=car_track_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@monitorController.post("/car_track/add", response_model=CrudResponse)
async def add_system_config(request: Request, add_car_track: CarTrackModel, query_db: Session = Depends(get_db)):
    try:
        add_car_track.create_by = "system"
        add_car_track.update_by = "system"
        add_config_result = await CarTrackService.add_car_track_services(request, query_db, add_car_track)
        if add_config_result.is_success:
            logger.info(add_config_result.message)
            return response_200(data=add_config_result, message=add_config_result.message)
        else:
            logger.warning(add_config_result.message)
            return response_400(data="", message=add_config_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@monitorController.post("/add_message")
async def add_message():
    try:
        AlertMessageService.add_message()
        return response_200(data="success", message="")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@monitorController.post("/upload_point")
async def upload_point():
    try:
        AlertMessageService.upload_point()
        return response_200(data="success", message="")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

@monitorController.post("/query_all_extence")
async def query_all_extence():
    try:
        AlertMessageService.query_all_extence()
        return response_200(data="success", message="")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

@monitorController.post("/refresh_task")
async def refresh_task(request: Request, add_car_track: CarTrackModel, result_db: Session = Depends(get_db)):
    try:
        await SchedulerUtil.init_system_scheduler()
        return response_200(data="success", message="")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@monitorController.post("/car_track/query_fence_terminal_status")
async def add_system_config(request: Request, tid: str, gfids: str, query_db: Session = Depends(get_db)):
    try:
        status = GaodeFenceTerminalService.query_fence_terminal_status("1033614", tid, gfids)
        return response_200(data=status, message="")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

@monitorController.get("/car_track/get_cms_terminal_point_by_terminal_id_last_one/{device_id}")
async def get_cms_terminal_point_by_terminal_id_last_one(request: Request, device_id: str, query_db: Session = Depends(get_db)):
    try:
        one = CmsTerminalPointDao.get_cms_terminal_point_by_terminal_id_last_one(query_db, device_id)
        return response_200(data=one, message="")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))



@monitorController.post("/car_track/query_fence_terminal")
async def add_system_config(request: Request, gfids: str, query_db: Session = Depends(get_db)):
    try:
        status = GaodeFenceTerminalService.query_fence_terminal("1033614", gfids)
        return response_200(data=status, message="")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
